package org.example.salesystem.controller;

import org.example.salesystem.entity.ResultBody;
import org.example.salesystem.service.impl.OrderServiceimpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/OrderMessage")
@CrossOrigin(origins="*")
public class OrderController {
    @Autowired
    private OrderServiceimpl orderService;

    //增加商品订单（卖家）
    @RequestMapping("/addOrder")
    public ResultBody addOrder(@RequestBody Map<String, Object> map) {
        //判断参数是否正确
        if (map.get("userphone") == null || map.get("GoodsCode") == null || map.get("GoodsName") == null || map.get("price") == null || map.get("OrderCode") == null|| map.get("buyphone") == null) {
            return new ResultBody(404, "缺少参数", false);
        }
        String userphone = (String) map.get("userphone");//买家电话号码
        String Goods_code = (String) map.get("GoodsCode");//商品编号
        String Goods_name = (String) map.get("GoodsName");//商品名称
        String OrderCode = (String) map.get("OrderCode");//订单编号
        String buyphone=(String) map.get("buyphone");
        Object object = map.get("price");//商品价格
        Double total_amount;
        if (object instanceof Integer) {
            total_amount = ((Integer) object).doubleValue();
        } else if (object instanceof Double) {
            total_amount = (Double) object;
        } else if (object instanceof String) {
            total_amount = Double.parseDouble((String) object);
        } else {
            return new ResultBody(400, "价格格式错误", false);
        }
        return orderService.addOrder(OrderCode, userphone, Goods_code, total_amount, Goods_name,buyphone);
    }
//展示订单信息（买家）
    @RequestMapping("/displayOrderU")
    public ResultBody displayOrder(@RequestBody Map<String, Object> map) {
        if (map.get("userphone") == null) {
            return new ResultBody(404, "缺少参数", false);
        }
        String user_phone = (String) map.get("userphone");
        return orderService.displayOrderU(user_phone);
    }
//展示订单信息（卖家）
    @RequestMapping("/displayOrderB")
    public ResultBody displayOrderB(@RequestBody Map<String, Object> map) {
        if (map.get("buyphone") == null) {
            return new ResultBody(404, "缺少参数", false);
        }
        String Buyers_phone = (String) map.get("buyphone");
        return orderService.displayOrderB(Buyers_phone);
    }
    //已发货/已收货
    @RequestMapping("/updateOrderStatus")
    public ResultBody updateOrderStatus(@RequestBody Map<String, Object> map) {
        if (map.get("code") == null || map.get("status") == null||map.get("Expresscode")==null) {
            return new ResultBody(404, "缺少参数", false);
        }
        return orderService.updateOrderStatus(map);
    }

    //展示待发货，已发货订单
    @RequestMapping("/displayOrderDY")
    public ResultBody displayOrderDY(@RequestBody Map<String,Object>map) {
        if(map.get("Buyphone")==null)
        {
            return new ResultBody(404,"缺少参数",false);
        }
        return orderService.displayOrderDY(map);
    }


}
